// miniprogram/pages/gameSignUpDetail/gameSignUpDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    gameTitle: "",
    gameId: "",
    user: [],
    tutor: "",
    gameCode: "",
    inputCode: "",
    gameAttention: "",
    // fileID: "",
    gradeTypes: ["初一", "初二", "初三", "高一", "高二", "高三", "大一", "大二", "大三", "大四"],
    groupTypes: [],
    gradeIndex: 0,
    groupIndex: 0,
    gradeType: "点击选择年级",
    groupType: "点击选择组别",
  },

  // change function begin
  changeGradePicker: function (e) {
    this.setData({
      gradeIndex: e.detail.value
    })

    this.setData({
      gradeType: this.data.gradeTypes[this.data.gradeIndex]
    })
  },

  changeGroupPicker: function (e) {
    this.setData({
      groupIndex: e.detail.value
    })

    this.setData({
      groupType: this.data.groupTypes[this.data.groupIndex]
    })
  },

  changeTutor: function (e) {
    this.setData({
      tutor: e.detail.value
    })
  },

  changeCode: function (e) {
    this.setData({
      inputCode: e.detail.value
    })
  },

  // change function end

  // 保存信息
  confirmSignUp: function () {
    // 如果在user中没有找到该用户
    console.log(this.data.user)
    if (this.data.user.length == 0) {
      wx.showModal({
        title: '报名错误',
        content: '请先在"个人资料"页面完善个人信息',
        showCancel: false,
      })
      return null
    }
    // console.log(this.data.gradeType[this.data.gradeIndex])
    // console.log(this.data.groupType[this.data.groupIndex])
    // console.log(this.data.tutor)
    // console.log(this.data.user)

    // 判断有无选择picker
    if (this.data.gradeType == "点击选择年级") {
      wx.showToast({
        title: '请选择年级',
        image: "/images/unhappy.png"
      })
      return;
    }

    if (this.data.groupType == "点击选择组别") {
      wx.showToast({
        title: '请选择组别',
        image: "/images/unhappy.png"
      })
      return;
    }

    let that = this
    if (that.data.inputCode == that.data.gameCode) {
      wx.cloud.callFunction({
        name: "addSignUp",
        data: {
          gameTitle: that.data.gameTitle,
          gameId: that.data.gameId,
          grade: that.data.gradeType,
          group: that.data.groupType,
          tutor: that.data.tutor,
          user: that.data.user
        },
        success: res => {
          if (res.result.length == 1) {
            console.log("报名已审核，修改报名信息失败", res)
            wx.showModal({
              title: '修改失败',
              content: '报名已通过审核，请联系负责人',
              showCancel: false,
            })
          } else {
            console.log("调用云函数插入报名数据成功", res)
            wx.showToast({
              title: '报名成功',
            })
          }
        },
        fail: res => {
          console.log("调用云函数插入报名数据失败", res)
        }
      })
    }
    else {
      wx.showToast({
        title: '比赛码错误',
        image: '/images/unhappy.png'
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var tempGroup = JSON.parse(options.group)
    // console.log(tempGroup)
    this.setData({
      gameTitle: options.title,
      gameId: options.id,
      // fileID: options.fileID,
      gameAttention: options.attention,
      groupTypes: tempGroup,
      gameCode: options.gameCode,
    })

    let secondTempGroup = []
    for(var i = 0; i < this.data.groupTypes.length; ++i) {
      if (this.data.groupTypes[i] != null)
        secondTempGroup.push(this.data.groupTypes[i])
    }
    // console.log(secondTempGroup)
    this.setData({
      groupTypes: secondTempGroup
    })

    // 换行符转义（将分号替换为\n）
    console.log(this.data.gameAttention)
    let temp = this.data.gameAttention.replace(/;/g, "\n")
    this.setData({
      gameAttention:temp
    })
    // console.log(this.data.gameAttention)

    // 获取openid
    let that = this
    wx.cloud.callFunction({
      name: "getOpenId",
      success: res => {
        console.log("获取openId成功", res.result.openid)
        // 根据openId获取用户信息
        // console.log(res.result.openid, "woxiaole")
        wx.cloud.callFunction({
          name: "getUserByOpenId",
          data: {
            openId: res.result.openid
          },
          success: res => {
            console.log("调用云函数获取用户信息成功", res)
            that.setData({
              user: res.result.data[0]
            })
            // console.log(that.data.user, "woxiaole")
          },
          fail: res => {
            console.log("调用云函数获取用户信息失败", res)
          }
        })
      },
      fail: res => {
        console.log("获取openId失败", res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})